package xcmg.device.service.agentReturn;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.agentReturn.AgentReturnDetailDO;
import xcmg.device.dao.entity.warehouse.WarehouseDO;
import xcmg.device.dao.mapper.MaterialLocationRelationMapper;
import xcmg.device.dao.mapper.agentReturn.AgentReturnDetailDOMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseDOMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.agentReturn.AgentReturnDetailAddVO;
import xcmg.device.service.vo.agentReturn.AgentReturnDetailShowVO;
import xcmg.device.service.vo.agentReturn.AgentReturnShowVO;
import xcmg.device.service.vo.basic.MaterialLocationRelationVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.log.LogHelper;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: xiali@bkrwin.com
 * @description:
 * @date： 2019/6/20 9:54
 **/
@Service
public class AgentReturnDetailService {
    @Autowired
    private FastGenClient fastGenClient;
    @Autowired
    private AgentReturnDetailDOMapper agentReturnDetailDOMapper;
    @Autowired
    private MaterialLocationRelationMapper materialLocationRelationMapper;

    @Autowired
    private WarehouseDOMapper warehouseDOMapper;
    @Autowired
    private AgentReturnService agentReturnService;
    /**
     * 新增代理商退货详情
     *
     * @param agentReturnDetailAddVO
     * @return ErrorCode
     */

    public ErrorCode addOrModify(AgentReturnDetailAddVO agentReturnDetailAddVO)throws Exception{
        AgentReturnDetailDO agentReturnDetailDO= new AgentReturnDetailDO();
        if (agentReturnDetailAddVO.getId()==null){
            BeanUtils.copyProperties(agentReturnDetailAddVO, agentReturnDetailDO);
            agentReturnDetailDO.setId(fastGenClient.textGuid().getValue());
            Boolean success =agentReturnDetailDOMapper.insertSelective(agentReturnDetailDO)>0;
            if(!success){
                LogHelper.error("新增代理商退货详情失败", ErrorCode.Failure.getCode());
                throw new Exception("新增代理商退货详情失败");
            }
            return ErrorCode.Success;
        }else{
            BeanUtils.copyProperties(agentReturnDetailAddVO, agentReturnDetailDO);
            Boolean success =agentReturnDetailDOMapper.updateByPrimaryKeySelective(agentReturnDetailDO)>0;
            if(!success){
                LogHelper.error("修改代理商退货详情失败", ErrorCode.Failure.getCode());
                throw new Exception("修改代理商退货详情失败");
            }
            return ErrorCode.Success;
        }
    }

    public FunctionResult<List<AgentReturnDetailShowVO>> list(String returnId){
        FunctionResult<List<AgentReturnDetailShowVO>> result = new FunctionResult<>();
        List<AgentReturnDetailDO> doList = agentReturnDetailDOMapper.getListByReturnId(returnId);
        List<AgentReturnDetailShowVO> voList = new ArrayList<>();
        for (AgentReturnDetailDO entity : doList) {
            AgentReturnDetailShowVO model = new AgentReturnDetailShowVO();
            BeanUtils.copyProperties(entity, model);
            voList.add(model);
        }
        result.setCode(ErrorCode.Success);
        result.setValue(voList);
        return result;
    }

    public AgentReturnShowVO item(String returnId){
        AgentReturnShowVO agentReturnShowVO = agentReturnService.item(returnId);
        if (null == agentReturnShowVO){
            return null;
        }
        String warehouseCode = agentReturnShowVO.getWarehouseCode();
        String orgId = agentReturnShowVO.getOrgId();
        FunctionResult<List<AgentReturnDetailShowVO>> functionResult = this.list(returnId);
        List<AgentReturnDetailShowVO> voList = functionResult.getValue();
        voList.forEach(x -> {
                getDefaultLocationCode(orgId, warehouseCode, x);
        });
        agentReturnShowVO.setDetailVOList(voList);
        if (StringUtils.isNotBlank(agentReturnShowVO.getWarehouseCode())) {
            WarehouseDO warehouse = warehouseDOMapper.itemByWarehouseCode(orgId, agentReturnShowVO.getWarehouseCode());
            if (null != warehouse) {
                agentReturnShowVO.setWarehouseCodeDesc(warehouse.getDescription());
            }
        }
        return agentReturnShowVO;
    }

    // 获取默认储位
    private void getDefaultLocationCode(String orgId, String warehouseCode, AgentReturnDetailShowVO detailVO) {
        MaterialLocationRelationVO condition = new MaterialLocationRelationVO();
        condition.setWarehouseCode(warehouseCode);
        condition.setOrgId(orgId);
        condition.setMaterialNo(detailVO.getMaterialNo());
        List<MaterialLocationRelationVO> voList = materialLocationRelationMapper.list(condition);
        if (!voList.isEmpty()) {
            detailVO.setLocationCode(voList.get(0).getLocationCode());
        }
    }
}
